/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditorPanel.java
 *
 * Created on 24.5.2009, 14:18:59
 */
package org.marvec.pisnickar.panels;

import java.io.IOException;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.marvec.pisnickar.songs.Song;
import org.marvec.pisnickar.songs.SongSource;
import org.marvec.pisnickar.tabs.TabManipulator;

/**
 *
 * @author marvec
 */
public class EditorPanel extends javax.swing.JPanel {
    TabManipulator manipulator;
    SongSource source;
    String songId;
    Song song;

    /** Creates new form EditorPanel */
    public EditorPanel(TabManipulator manipulator, SongSource source, String songId, Song song) {
        initComponents();
        this.manipulator = manipulator;
        this.source = source;
        this.songId = songId;
        this.song = song;

        titleTextField.setText(song.getTitle());
        musicTextField.setText(song.getAuthorMusic());
        textTextField.setText(song.getAuthorText());
        tagsTextField.setText(song.getTagsString());
        if (song.getText() != null && !"".equals(song.getText())) {
            mainTextPane.setText(song.getText());
        }
        titleTextField.requestFocusInWindow();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        titleTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        tagsTextField = new javax.swing.JTextField();
        saveCloseButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        mainTextPane = new javax.swing.JTextPane();
        cancelButton = new javax.swing.JButton();
        saveShowButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        musicTextField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        textTextField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(org.marvec.pisnickar.PisnickarApp.class).getContext().getResourceMap(EditorPanel.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        titleTextField.setText(resourceMap.getString("titleTextField.text")); // NOI18N
        titleTextField.setName("titleTextField"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        tagsTextField.setText(resourceMap.getString("tagsTextField.text")); // NOI18N
        tagsTextField.setName("tagsTextField"); // NOI18N

        saveCloseButton.setText(resourceMap.getString("saveCloseButton.text")); // NOI18N
        saveCloseButton.setName("saveCloseButton"); // NOI18N
        saveCloseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveCloseButtonActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        mainTextPane.setFont(new java.awt.Font("DejaVu Sans Mono", 0, 12));
        mainTextPane.setText(resourceMap.getString("mainTextPane.text")); // NOI18N
        mainTextPane.setName("mainTextPane"); // NOI18N
        jScrollPane1.setViewportView(mainTextPane);

        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        saveShowButton.setText(resourceMap.getString("saveShowButton.text")); // NOI18N
        saveShowButton.setName("saveShowButton"); // NOI18N
        saveShowButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveShowButtonActionPerformed(evt);
            }
        });

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        musicTextField.setText(resourceMap.getString("musicTextField.text")); // NOI18N
        musicTextField.setName("musicTextField"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        textTextField.setText(resourceMap.getString("textTextField.text")); // NOI18N
        textTextField.setName("textTextField"); // NOI18N

        jLabel5.setFont(jLabel5.getFont().deriveFont(jLabel5.getFont().getSize()-2f));
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addContainerGap(426, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addContainerGap(430, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addContainerGap(437, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(musicTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
                    .addComponent(titleTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
                    .addComponent(textTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE)
                    .addComponent(tagsTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 409, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(331, Short.MAX_VALUE)
                .addComponent(jLabel5)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(132, Short.MAX_VALUE)
                .addComponent(cancelButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(saveShowButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(saveCloseButton)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 485, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(titleTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(musicTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(textTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tagsTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 237, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveCloseButton)
                    .addComponent(saveShowButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private boolean save() {
        song.setTitle(titleTextField.getText());
        song.setAuthorMusic(musicTextField.getText());
        song.setAuthorText(textTextField.getText());
        song.setTags(tagsTextField.getText().split(", "));
        song.setText(mainTextPane.getText());
        if (song.isValid()) {
            try {
                songId = source.storeSong(songId, song);
                manipulator.updateEditTitle(song.getTitle());
                return true;
            } catch (IOException ex) {
                Logger.getLogger(EditorPanel.class.getName()).log(Level.SEVERE,
                        "Cannot save changes to the song " + songId,
                        ex);
                JOptionPane.showMessageDialog(this, "Změny se nepodařilo uložit.", "Chyba při ukládání",
                        JOptionPane.ERROR_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(this, "Písnička není dostatečně zadána. Název a text nesmí být prázdné.",
                    "Chyba při ukládání", JOptionPane.ERROR_MESSAGE);
        }
        return false;
    }

    private void saveCloseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveCloseButtonActionPerformed
        if (save()) {
            manipulator.closeCurrentTab();
        }
    }//GEN-LAST:event_saveCloseButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        if (JOptionPane.showConfirmDialog(this, "Skutečně chcete zahodit změnit a zavřít záložku?", "Zrušit úpravy",
                JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            manipulator.closeCurrentTab();
        }
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void saveShowButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveShowButtonActionPerformed
        if (save()) {
            manipulator.openUrl(TabManipulator.formatSongUrl(source.getId(), songId));
        }
    }//GEN-LAST:event_saveShowButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextPane mainTextPane;
    private javax.swing.JTextField musicTextField;
    private javax.swing.JButton saveCloseButton;
    private javax.swing.JButton saveShowButton;
    private javax.swing.JTextField tagsTextField;
    private javax.swing.JTextField textTextField;
    private javax.swing.JTextField titleTextField;
    // End of variables declaration//GEN-END:variables
}
